package com.ly.designpatten.proxy.cglib;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import	java.util.stream.Collectors;

import lombok.AllArgsConstructor;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Classname ProxyFactory
 * @Description TODO
 * @Date 2020/10/16 19:22
 * @Author 冷心影翼
 */
@AllArgsConstructor
public class ProxyFactory implements MethodInterceptor {

	private Object target;

	public Object getProxyInstance() {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(target.getClass());
		enhancer.setCallback(this);
		return enhancer.create();
	}

	@Override
	public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
		System.out.println("Boss:");
		return method.invoke(target,args);
	}

	public static void main(String[] args) {
		Boss boss = new Boss();
		boss = (Boss) new ProxyFactory(boss).getProxyInstance();
		boss.publish();



	}
}
